﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HerGoS.ControlsUI;
using HerGoS.Viaticos.Entities;
using HerGoS.Viaticos.Engine;
using HerGoS.WebControlExtend;

namespace HerGoS.WebUI.Viaticos
{
    public partial class Solicitudes_Listado : PageBase
    {
        
        #region Propiedades
        public List<Solicitud> Solicitudes
        {
            get
            {
                if (ViewState["Solicitud"] != null)
                    return (List<Solicitud>)ViewState["Solicitud"];
                return null;
            }
            set
            {
                ViewState["Solicitud"] = value;
            }

        }
        #endregion

        #region EvalPersona

        protected string GetPersona()
        {
            return " " + Eval("Apellidos") + " " + Eval("Nombres");

        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!Page.IsPostBack)
            {
                Initialize();
                LoadGridSolicitud(false);
            }

        }

        void Initialize()
        {
            this.GridSolicitudes.Settings(new GridViewExtBasic());
            this.btnNuevo.Settings(new ButtonExtText());
            this.btnRefresh.Settings(new ButtonExtText());
            this.btnFiltrar.Settings(new ButtonExtText());
            this.bntQuitarFiltros.Settings(new ButtonExtText());
        }

        private void LoadGridSolicitud(bool cache)
        {
            if (!cache)
                this.Solicitudes = GestorSolicitudes.Consultar();
            this.GridSolicitudes.DataSource = Solicitudes;
            this.GridSolicitudes.DataBind();
        }


        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadGridSolicitud(false);
        }



        protected void btnBorrar_Click(object sender, EventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            int index = Convert.ToInt32(btn.CommandArgument);
            int id = Convert.ToInt32(this.GridSolicitudes.DataKeys[index].Value);

            Solicitud solicitud = Solicitudes.Find(c => c.OID == id);
            Solicitudes.Remove(solicitud);

            GestorSolicitudes.Eliminar(solicitud);
            LoadGridSolicitud(true);

        }

        protected void GridSolicitudes_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButtonExt Editar = e.Row.FindControl("btnEditar") as ImageButtonExt;
                if (Editar != null)
                {
                    Editar.Settings(new ImageButtonExtEdit());
                    int oid = DataBinder.Eval(e.Row.DataItem, "OID").ToInteger();
                    Editar.OnClientClick = "Editar(" + oid.ToString() + "); return false;";
                }

                ImageButtonExt Borrar = e.Row.FindControl("btnBorrar") as ImageButtonExt;
                if (Borrar != null)
                {
                    Borrar.Settings(new ImageButtonExtDelete());
                }
            }
        }

        protected void GridSolicitudes_PageIndexChanging(Object sender, GridViewPageEventArgs e)
        {
            this.GridSolicitudes.PageIndex = e.NewPageIndex;
            LoadGridSolicitud(true);
        } 


        protected void btnFiltrar_Click(object sender, EventArgs e)
        {
            List<Solicitud> filtroResult = null; 
                        

            if (!string.IsNullOrEmpty(this.txtNumeroSolicitud.Text))
            {
                filtroResult = Solicitudes.FindAll(y => y.Numero == Convert.ToInt32(this.txtNumeroSolicitud.Text));
            }

            if (!string.IsNullOrEmpty(this.txtArea.Text))
            {
                filtroResult = Solicitudes.FindAll(y => y.Area != null).FindAll(y => y.Area.Descripcion.ToLower().Contains(txtArea.Text.ToLower()));
            
            }


            if (!string.IsNullOrEmpty(this.txtApyNombre.Text))
            {
                filtroResult = Solicitudes.FindAll(y => y.Persona != null).FindAll(y => y.Persona.NombresYApellidos.ToLower().Contains(txtApyNombre.Text.ToLower()));
            
            }



            

            if (!string.IsNullOrEmpty(this.txtNumeroSolicitud.Text) &&  (!string.IsNullOrEmpty(this.txtArea.Text)) &&  (!string.IsNullOrEmpty(this.txtApyNombre.Text)))
            {

                filtroResult = Solicitudes.FindAll(y => y.Area != null).FindAll(y => y.Area.Descripcion.ToLower().Contains(txtArea.Text.ToLower()))
                 .FindAll(y => y.Numero == Convert.ToInt32(this.txtNumeroSolicitud.Text))
                 .FindAll(y => y.Persona != null).FindAll(y => y.Persona.NombresYApellidos.ToLower().Contains(txtApyNombre.Text.ToLower()));

           
            }
           
          


            this.GridSolicitudes.DataSource = filtroResult;
            this.GridSolicitudes.DataBind();
        }
        protected void btnQuitarFiltros_Click(object sender, EventArgs e)
        {
            this.txtApyNombre.Text = string.Empty;
            this.txtArea.Text = string.Empty;
            this.txtNumeroSolicitud.Text = string.Empty;
            LoadGridSolicitud(true);

        }

    }
}